#ifndef __MP_UDP_CLIENT_H__
#define __MP_UDP_CLIENT_H__

#include "net_base.h"
#include "eventloop.h"
#include "udp_client_context.h"

#include <functional>

namespace Gazella {

typedef std::function<void(std::string&)>             udpclient_recv_over_callback_t;
typedef std::function<void(std::string, int, size_t)> udpclient_send_over_callback_t;

class UdpClient: public NetBase {
public:
    UdpClient();
    virtual ~UdpClient();

    //init
    int init(std::string host, int port) override;

    void send_message(std::string message, bool directly = false);

    void add_recv_over_callback(udpclient_recv_over_callback_t callback);
    void add_send_over_callback(udpclient_send_over_callback_t callback);

protected:
    int m_sd = -1;

    std::shared_ptr<UdpClientContext> m_udp_client_context;

private:
    int __create_udp_client(std::string host, int port);

    void __recv_cb(ev::io &w, int revents) override;
    void __send_cb(ev::io &w, int revents) override;

    virtual void __on_message_recv_over();
    virtual void __on_message_send_over();

private:
    size_t m_buffer_size = 64 * 1024; //udp package max size
    char *m_buffer = nullptr;

    udpclient_recv_over_callback_t m_recv_over_callback = nullptr;
    udpclient_send_over_callback_t m_send_over_callback = nullptr;
};
}

#endif